<template>
    <div class="edit-user">
      <h2>编辑用户</h2>
      <el-form :model="editUser" ref="editUserForm" class="form-container">
        <!-- 用户名输入框 -->
        <div class="form-row">
          <el-form-item label="用户名" :label-width="'120px'" class="form-label">
            <el-input v-model="editUser.username" placeholder="请输入用户名" />
          </el-form-item>
        </div>
  
        <!-- 邮箱输入框 -->
        <div class="form-row">
          <el-form-item label="邮箱" :label-width="'120px'" class="form-label">
            <el-input v-model="editUser.email" placeholder="请输入邮箱" />
          </el-form-item>
        </div>
  
        <!-- 用户角色选择 -->
        <div class="form-row">
          <el-form-item label="角色" :label-width="'120px'" class="form-label">
            <el-select v-model="editUser.role" placeholder="请选择角色">
              <el-option
                v-for="role in roles"
                :key="role.value"
                :label="role.label"
                :value="role.value"
              />
            </el-select>
          </el-form-item>
        </div>
  
        <!-- 提交和取消按钮 -->
        <div class="form-actions">
          <el-button type="primary" @click="submitEditUser" class="submit-btn">提交修改</el-button>
          <el-button @click="cancelEdit" class="cancel-btn">取消</el-button>
        </div>
      </el-form>
    </div>
  </template>
  
  <script>
  import axios from "axios";
  
  export default {
    data() {
      return {
        roles: [
          { value: "admin", label: "管理员" },
          { value: "editor", label: "编辑" },
          { value: "viewer", label: "查看者" },
        ],
        editUser: {
          ID: null,
          username: "",
          email: "",
          role: "",
        },
      };
    },
    mounted() {
      this.loadUser();
    },
    methods: {
      // 加载用户数据
      async loadUser() {
        const userId = this.$route.params.id; // 从路由中获取用户ID
        console.log("加载用户，ID:", userId);
        try {
          const response = await axios.get(`http://localhost:9090/users/${userId}`);
          console.log("用户数据:", response.data.data);
          this.editUser = response.data.data; // 将返回的数据赋值给 editUser
        } catch (error) {
          console.error("加载用户失败", error);
          alert("加载用户失败");
        }
      },
  
      // 提交修改后的用户信息
      async submitEditUser() {
        try {
          const response = await axios.put(
            `http://localhost:9090/users/${this.editUser.ID}`,
            this.editUser
          );
          if (response.status === 200) {
            alert("用户修改成功");
            this.$router.push("/user_management"); // 修改成功后跳转到用户管理页面
          } else {
            alert("修改用户失败");
          }
        } catch (error) {
          console.error("修改用户失败", error);
          alert("修改用户失败");
        }
      },
  
      // 取消编辑
      cancelEdit() {
        this.$router.push("/user_management"); // 取消编辑跳转回用户管理页面
      },
    },
  };
  </script>
  
  <style scoped>
  .edit-user {
    width: 80%;
    margin: 30px auto;
    padding: 20px;
    background-color: #f9f9f9;
    border-radius: 8px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  }
  
  h2 {
    text-align: center;
    color: #333;
    margin-bottom: 20px;
  }
  
  .form-container {
    background-color: #fff;
    padding: 30px;
    border-radius: 8px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  }
  
  .form-row {
    display: flex;
    justify-content: space-between;
    margin-bottom: 20px;
  }
  
  .form-label {
    width: 20%;
  }
  
  .el-form-item {
    flex: 1;
  }
  
  .form-actions {
    text-align: center;
    margin-top: 20px;
  }
  
  .submit-btn {
    background-color: #409eff;
    color: white;
    margin-right: 10px;
  }
  
  .cancel-btn {
    background-color: #f5f7fa;
    color: #409eff;
  }
  
  .el-input,
  .el-select {
    width: 100%;
  }
  
  .el-form-item label {
    color: #666;
  }
  </style>
  